1. Introduction to Loop Statements
Loop statements in C programming allow you to execute a block of code repeatedly based on a condition. They are essential for automating repetitive tasks, processing data structures, and implementing algorithms that require iteration.
Note: Loops are fundamental to programming as they help reduce code redundancy and make programs more efficient and readable.
2. Types of Loop Statements
2.1 for Loop
Definition: The for loop is used when you know in advance how many times you want to execute a statement or a block of statements. It is counted , entry control looping structure.
Syntax:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Example:
#include <stdio.h>
int main() {
int i;
// Print numbers from 1 to 5
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4 5
2.2 while Loop
Definition: The while loop repeatedly executes a target statement as long as a given condition is true. It is entry control ,conditional looping structure.
Syntax:
while (condition) {
// code to be executed
}
Example:
#include <stdio.h>
int main() {
int count = 1;
// Print numbers from 1 to 5
while (count <= 5) {
printf("%d ", count);
count++;
}
return 0;
}
Output:
1 2 3 4 5
2.3 do-while Loop
Definition: The do-while loop is similar to a while loop, except that it tests the condition at the end of the loop body, ensuring the loop executes at least once. It is exit control , conditional looping structure.
Syntax:
do {
// code to be executed
} while (condition);
Example:
#include <stdio.h>
int main() {
int num = 6;
// This will execute at least once
do {
printf("%d ", num);
num++;
} while (num <= 5);
return 0;
}
Output:
6
2.4 Nested Loops
Definition: A nested loop is a loop inside the body of another loop. The inner loop will be executed completely for each iteration of the outer loop.
Example:
#include <stdio.h>
int main() {
int i, j;
// Print a multiplication table
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
printf("%d x %d = %d\t", i, j, i * j);
}
printf("\n");
}
return 0;
}
Output:
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9
3. Loop Control Statements
3.1 break Statement
Definition: The break statement terminates the loop immediately when encountered.
Example:
#include <stdio.h>
int main() {
int i;
// Break the loop when i equals 3
for (i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
1 2
3.2 continue Statement
Definition: The continue statement skips the current iteration of the loop and continues with the next iteration.
Example:
#include <stdio.h>
int main() {
int i;
// Skip the iteration when i equals 3
for (i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 4 5
3.3 goto Statement
Definition: The goto statement provides an unconditional jump from the goto to a labeled statement in the same function.
Example:
#include <stdio.h>
int main() {
int i = 1;
loop:
if (i <= 5) {
printf("%d ", i);
i++;
goto loop;
}
return 0;
}
Output:
1 2 3 4 5
Note: While goto can be useful in some cases, it's generally discouraged as it can make code harder to read and maintain.
6. Common Mistakes
1. Infinite loops
// This will run forever
int i = 0;
while (i < 5) {
printf("%d ", i);
// Forgot to increment i
}
2. Off-by-one errors
// This will print 0 to 4 instead of 1 to 5
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
3. Semicolon after loop statement
// This will not print anything
for (int i = 0; i < 5; i++); {
printf("%d ", i);
}
7. Real-world Examples
Example 1: Calculating Factorial
#include <stdio.h>
int main() {
int num, i;
long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
// Calculate factorial
for (i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d = %lld\n", num, factorial);
return 0;
}
Example 2: Fibonacci Series
#include <stdio.h>
int main() {
int n, i;
long long t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%lld, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Example 3: Prime Number Check
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
// Check if num is prime
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime && num > 1) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}